<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>
<script>
export default {
  data(){
    return{
      mac:"",
      socketClient:null
    }
  },
  mounted(){
   this.initWebSocket()
  },
  methods:{
    initWebSocket() {
      let mac=this.$util.getMac()
      this.socketClient = new WebSocket(`${process.env.VUE_APP_SOCKET}${mac}`);
      this.socketClient.onopen = this.websocketOnopen;
      this.socketClient.onmessage = this.websocketOnmessage;
      this.socketClient.onclose = this.websocketOnclose;
      this.socketClient.onerror = this.websocketOnerror;
    },
    websocketOnerror(e){
      console.log("WebSocket连接错误",e);
    },
    websocketOnopen(e) {
      console.log("WebSocket连接成功");
    },
    websocketOnmessage(e) {
      let typeList=['mode_jiaoyu','mode_dangjian1','mode_qinglian']
      if(e.data==='mode_dangjian1'){
        this.$router.push(`/dangjian`)
      }else if(e.data==='mode_jiaoyu'){
        this.$router.push(`/jiaoyu`)
      }else if(e.data==='mode_qinglian'){
        this.$router.push(`/qinglian`)
      }
    },

    websocketOnclose(e) {
      this.socketClient.close();
      console.log("WebSocket closed",e);
      this.websocketReconnect();
    },
    websocketReconnect() {
      setTimeout(() => {
        this.initWebSocket();
      }, 5000);
    },
  }
}
</script>
<style lang="scss">

</style>
